#!/usr/bin/php
<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require("bashcommon.php");
$logid = startlog("reminder");
$blog =& LoggerManager::getLogger('bash_reminder');
$blog->info("Started");
$_log->info("Started");

$blog->info("File Assign Control"); 

$p = new project;
$p->status=STATUS_ACTIVE;
$prjlist = $loader->loadListBy($p,$p->listByStatus(true));

$notification = new maplist;

function addfile($userid,$file,$prj) {
	global $blog;
	global $notification;
	
	$elem = new stdClass;
	$elem->file = $file;
	$elem->prj = $prj;
	$notification->add($userid,$elem);
}

foreach ($prjlist as $prj) {
	$f = new files($prj->id);
	$f->statusid = array(FILE_STATUS_LOCKED,FILE_STATUS_CHKPND);
	$flist = $loader->loadListBy($f,$f->loadfiltered());
	foreach ($flist as $fil) {
		if ($fil->lastupdate > $fil->lastreminder) {
			if (util::isExpire($fil->lastupdate,ASSIGN_REMINDER) and util::isExpire($fil->lastreminder,ASSIGN_REMINDER)) {
				if ($fil->state()!=1) {
					if ($fil->userid!="") {
						addfile($fil->userid,$fil,$prj);
						$loader->execute($fil->reminder());
					} 
				} else {
					if ($fil->statusid==FILE_STATUS_CHKPND) {
						if ($fil->userid2!="") {
							addfile($fil->userid2,$fil,$prj);
							$loader->execute($fil->reminder());							
						}
					}
				}
			}
		}
	}
}

$blog->info("File notification count: ".$notification->size());
$_log->info("File notification count: ".$notification->size());

foreach ($notification->getAll() as $userid => $notif) {
	$u = new user;
	$u->userid=$userid;
	$loader->get($u);
	mailFileReminder($u,$notif);
}

$blog->info("Uploaded Elements");

$notification = new maplist;

$up = new upload;
$list = $loader->loadList($up);

foreach ($list as $u) {
	if (util::isExpire($u->udate,UPLOAD_REMINDER) and util::isExpire($u->lastcontrol,UPLOAD_REMINDER)) {
		if ($u->takeuser!=null) {
			$us = new user;
			$us->userid = $u->takeuser;
			$loader->get($us);
			
			$elem = new stdClass;
			$elem->upload=$u;
			$elem->user=$us;
			$notification->add($u->takeuser,$elem);
		} else {
			$us = new user;
			$us->teamid = $u->teamid;
			$us->groupid = array(GROUP_COORD,GROUP_SVN);
			$ulist = $loader->loadListBy($us, $us->listByFilters(null, "1006"));			
			foreach ($ulist as $uss) {
				$elem = new stdClass;
				$elem->upload=$u;
				$elem->user=$uss;				
				$notification->add($uss->userid,$elem);	
			}
		}
		$loader->execute($u->updateControl());
	}
}

$blog->info("Uploads notification count: ".$notification->size());
$_log->info("Uploads notification count: ".$notification->size());

foreach ($notification->getAll() as $el) {
	$flist=array();
	foreach ($el as $elem) {
		$us = $elem->user;
		$ud = new upload_data;
		$ud->objid=$elem->upload->id;
		$list = $loader->loadList($ud);
		foreach ($list as $udata) {
			$flist[]=$udata->name;
		}
	}
	mailUploadReminder($us,$flist);
}

$blog->info("Finished.");
$_log->info("Finished.");
endlog($logid);
$db->close();

?>